﻿/*
 * Summary:
 *   The FileReader class loads data from text file 
 *   and converts it to corresponding object for
 *   the sports store
 */

using System;
using System.Collections.Generic;
using System.Collections;  
using System.Linq;
using System.Text;
using System.IO;    /* YIWEI YU: for file/stream manipulation */

namespace AcmeSportsStore
{
    class FileReader
    {
        StreamReader mSrSportsItems;
        StreamReader mSrCustomers;
        StreamReader mSrPurchasedItems;
        
        /*
         *  constructor of FileReader
         */
        public FileReader(StreamReader srSportsItems, StreamReader srCustomers, 
            StreamReader srPurchasedItems)
        {
            mSrSportsItems = srSportsItems;
            mSrCustomers = srCustomers;
            mSrPurchasedItems = srPurchasedItems;
        }

        /*
         *read SportsItem.txt file
         */
        public SportsStore ReadSportsItems()
        {
            int i;  
            string[] splits;
            string readLine;

            SportsStore itemsIndexs = new SportsStore();
            readLine = mSrSportsItems.ReadLine();
            i = 0;
            while (readLine != null)
            {
                // Split out parts and create a sportItem object
                Console.WriteLine("The {0} line:", i);
                splits = readLine.Split(';');
       
                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }

                char idinit = splits[0][0];

                if (splits[0][0] == 'M')
                {   /* items without user gender specified */
                    itemsIndexs.AddItem(new SportsItem(
                        splits[0],
                        splits[1],
                        splits[2],
                        (int)(Convert.ToDecimal(splits[3]) * 100 + 0.5m)));
                }
                else if (splits[0][0] == 'G')
                {
                    SportsItemG.Gender gender;
                    if (splits[2] == "male")
                    {
                        gender = SportsItemG.Gender.Male;
                    }
                    else if (splits[2] == "female")
                    {
                        gender = SportsItemG.Gender.Female;
                    }
                    else
                    {
                        gender = SportsItemG.Gender.None;
                    }

                    itemsIndexs.AddItem(new SportsItemG(
                        splits[0],
                        splits[1],
                        splits[3],
                        (int)(Convert.ToDecimal(splits[4]) * 100 + 0.5m),
                        gender));
                }
                else
                {   /* invalid ID, just ignore */
                }

                readLine = mSrSportsItems.ReadLine();
               i++;
            }
            i = 0;
            return itemsIndexs;
        }

        /*
         * read  customers information
         */
        public Customers ReadCustomers()
        {
            int i;
            string[] splits;
            string readLine;

            Customers custIndexs = new Customers();
            readLine = mSrCustomers.ReadLine();
            i = 0;
            while (readLine != null)
            {
                // Split out parts and create a Customer object
                splits = readLine.Split(';');
                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }
                if (splits[0].Length > 0)
                {
                        custIndexs.AddItem(new CustomersItem(
                        Convert.ToInt32(splits[0]),
                        splits[1],
                        splits[2],
                        splits[3]));
                }
                else
                {   /* invalid ID, just ignore */
                }

               readLine = mSrCustomers.ReadLine();
                i++;
            }
            return custIndexs;
        }

        /*
         *Read purchase items
         */
        public PurchasedStore ReadPurchasedItems()
        {
            string[] splits;
            string readLine;

            PurchasedStore PurchasedIndexs = new PurchasedStore();
            readLine = mSrPurchasedItems.ReadLine();
            // for the unique Id for each purchased
            int uniqueId = 0;
            while (readLine != null)
            {
                // Split out parts and create a Customer object   
                splits = readLine.Split(';');
                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }
                if (splits[0].Length > 0)
                {
                    PurchasedIndexs.AddItem(new PurchasedItem(uniqueId,
                                                            splits[0],
                                                            splits[1],
                                                            splits[2],
                                                            splits[3],
                                                            splits[4]));
                }
                else
                {   /* invalid ID, just ignore */
                }
                readLine = mSrPurchasedItems.ReadLine();
                uniqueId++;
            }
            return PurchasedIndexs;
        }
        
    }
}
